package com.sanpu.kxe.business.main.view;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.fragment.AuthSecondStepFragment;
import com.sanpu.kxe.business.main.fragment.AuthThirdStepFragment;
import com.sanpu.kxe.business.main.interfaces.IOnSwitchPageListener;
import com.sanpu.kxe.common.custom.SerializableHashMap;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.jump.eventbus.ReviewPhotoEvent;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.FileUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Description：照片认证
 * Created by 薛勇军 on 2018/7/17.
 */

public class CertificateSecondActivity extends BaseTitleActivity {

    private AuthSecondStepFragment secondFragment;
    private AuthThirdStepFragment thirdStepFragment;

    private int mCurrentStep = 1;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_certificate_second;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("实名认证");
        secondFragment = new AuthSecondStepFragment();
        thirdStepFragment = new AuthThirdStepFragment();
        if (savedInstanceState != null) {
            List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
            for (Fragment fragment : fragmentList) {
                if (fragment instanceof AuthSecondStepFragment) {
                    secondFragment = (AuthSecondStepFragment) fragment;
                } else if (fragment instanceof  AuthThirdStepFragment) {
                    thirdStepFragment = (AuthThirdStepFragment) fragment;
                }
            }
            if (thirdStepFragment == null) {
                getSupportFragmentManager().beginTransaction().show(secondFragment).commit();
            } else {
                getSupportFragmentManager().beginTransaction().show(secondFragment).hide(thirdStepFragment).commit();
            }
        } else {  // 正常时
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.container, secondFragment);
            ft.commit();
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        secondFragment.setOnSwitchPageListener(new IOnSwitchPageListener() {
            @Override
            public void onSwitchPage() {
                try {
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.setCustomAnimations(
                            R.anim.in_from_right, R.anim.in_to_left,
                            R.anim.out_to_left, R.anim.out_from_right);
                    if (thirdStepFragment != null && thirdStepFragment.isAdded()) {
                        ft.show(thirdStepFragment).hide(secondFragment);
                    } else {
                        ft.add(R.id.container, thirdStepFragment).hide(secondFragment);
                    }
                    ft.commit();
                    mCurrentStep = 2;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (thirdStepFragment.isVisible()) {
            switchPageSecond();
        } else {
            DialogUtil.getInstance().dialogExistTitleTwoBtnHaveCallBack(mContext, "温馨提示",
                    "是否放弃这次认证操作", "取消", "放弃",
                    new DialogViewListener() {
                        @Override
                        public void btnRightConfirm() {
                            finish();
                        }

                        @Override
                        public void btnLeftCancel() {

                        }
                    });
        }
    }

    /**
     * 返回到认证第二步
     */
    public void switchPageSecond() {
        try {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.setCustomAnimations(
                    R.anim.out_to_left, R.anim.out_from_right,
                    R.anim.in_from_right, R.anim.in_to_left
            );
            ft.hide(thirdStepFragment);
            ft.show(secondFragment);
            ft.commit();
            mCurrentStep = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ReviewPhotoEvent event) {
        if (event != null) {
            switch (mCurrentStep) {
                case 1:
                    secondFragment.refreshView(event);
                    break;
                case 2:
                    thirdStepFragment.refreshView(event);
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (mCurrentStep) {
            case 1:
                secondFragment.onActivityResult(requestCode, resultCode, data);
                break;
            case 2:
                thirdStepFragment.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileUtil.deleteCache();
    }

    private CertificationOutput certificationOutput;

    public CertificationOutput getCertificationOutput() {
        return certificationOutput;
    }

    public void setCertificationOutput(CertificationOutput certificationOutput) {
        this.certificationOutput = certificationOutput;
    }

    private SerializableHashMap serializableHashMap;

    public SerializableHashMap getSerializableHashMap() {
        return serializableHashMap;
    }

    public void setSerializableHashMap(SerializableHashMap serializableHashMap) {
        this.serializableHashMap = serializableHashMap;
    }
}
